package eudjok

import java.util.*
import java.text.*
import groovy.lang.Closure.*

class YouTubeController {

	static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

	def index = {
        redirect(action: "buscar", params: params)
    }
	
	def buscar = {
	
		def videos = null
		if (params.key != null) {
			String key  = params.key
			
			System.out.println("============================ : " + key)
			
			def youTubeManagerInstance = new YouTubeManager()
			
			int maxResults = 24
			boolean filter = true
			int timeout = 2000
				
			videos = youTubeManagerInstance.retrieveVideos(key, maxResults, filter, timeout);
				
			for (YouTubeVideo youtubeVideo : videos) {
				
				String linkYoutube = youtubeVideo.getEmbeddedWebPlayerUrl();
				
				String[] addMusica = linkYoutube.split("&");

			}
				
		}
		[videos: videos]
		
	}
			
	def list = {
		params.max = Math.min(params.max ? params.int('max') : 10, 100)
		[youTubeInstanceList: youTube.list(params), youTubeInstanceTotal: youTube.count()]
	}
	
	
	
	def view = {
		def setListInstance = SetList.get(params.id)
		if (!setListInstance) {
			flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'setList.label', default: 'SetList'), params.id])}"
			redirect(action: "list")
		}
		else {
			[setListInstance: setListInstance]
		}
	}
	
	def buscarSL = {
		
		def setListInstance = SetList.findById(params.setListId)
		def videos = null
		
		
		if (params.key != null) {
			String key  = params.key
			
			def youTubeManagerInstance = new YouTubeManager()
			
			int maxResults = 25
			boolean filter = true
			int timeout = 2000
				
			videos = youTubeManagerInstance.retrieveVideos(key, maxResults, filter, timeout);
				
			for (YouTubeVideo youtubeVideo : videos) {
				
				String linkYoutube = youtubeVideo.getEmbeddedWebPlayerUrl();
				
				String[] addMusica = linkYoutube.split("&");
				
			}
				
		}
		[videos: videos, setListId:params.setListId, setListInstance:setListInstance]
		
	}
	
}